package com.faithbj.danaliverpool.web.controller.admin;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import com.faithbj.common.configuration.Pager;
import com.faithbj.common.model.Criterion;
import com.faithbj.danaliverpool.service.CriterionService;

/**
 * @author jrn E-mail: rinanjin@gmail.com
 * @version 0.1 
 * 创建时间：2012-6-8 下午11:43:29
 * 类说明
 */
@Controller
@RequestMapping("/faith/criterion")
public class AdminCriterionController extends BaseAdminController
{
	private static final long serialVersionUID = 1L;

	@Resource
	private CriterionService criterionService;
	
	private static final String LIST = "/admin/criterion_list";
	
	private static final String INPUT = "/admin/criterion_input";
	
	private static final String OPERRATE_RETURN_URL = "criterion/list";
	
	/**
	 * 处理时间的转换器，默认格式是yyyy-MM-dd HH:mm:ss可以被重载
	 * @param binder
	 */
	@Override
	@InitBinder
	public void initBinder(WebDataBinder binder)
	{
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, false));
	}
	
	@RequestMapping(value = "/list")
	public String list(Pager pager, ModelMap map)
	{
		Pager pagerVal = criterionService.findByPager(pager);
		map.put("pager", pagerVal);
		return LIST;
	}
	
	@RequestMapping(value = "/add")
	public String add(ModelMap map)
	{
		map.put("isAddAction", true);
		return INPUT;
	}
	
	@RequestMapping(value = "/save")
	public String save(Criterion criterion, ModelMap map)
	{
		criterion.setOperator(getLoginUser().getUsername());
		criterionService.save(criterion);
		criterionService.flush();
		
		map.put("redirectUrl", OPERRATE_RETURN_URL);
		return SUCCESS;
	}
}
